package com.liuxl.wqdc.service;

import com.liuxl.wqdc.entity.Exam;
import com.liuxl.wqdc.entity.OneLevelQuestion;
import com.liuxl.wqdc.entity.Record;
import com.liuxl.wqdc.entity.TwoLevelQuestion;
import com.liuxl.wqdc.repository.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


@Service
public class ExamService {
    @Autowired
    ExamRepository examRepository;
    @Autowired
    TwoLevelQuestionRepository twoLevelQuestionRepository;
    @Autowired
    OneLevelQuestionRepository oneLevelQuestionRepository;
    @Autowired
    RecordRepository recordRepository;
    @Autowired
    BatchInsertRepository batchInsertRepository;

    public List<Exam> findAll() {
        List<Exam> examList = examRepository.findAll();
        return examList;
    }

    public JSONObject getExamDetails(Integer examId) {
        JSONArray resultArr = new JSONArray();
        Optional<Exam> exam = examRepository.findById(examId);
        List<OneLevelQuestion> oneLevelQuestionList = oneLevelQuestionRepository.findByExamId(examId);
        List<Integer> oneQuestionIdList = new ArrayList<>();
        for (OneLevelQuestion oneLevelQuestion : oneLevelQuestionList) {
            oneQuestionIdList.add(oneLevelQuestion.getId());
        }
        List<TwoLevelQuestion> twoLevelQuestionList = twoLevelQuestionRepository.findByOneLevelQuestionIdIn(oneQuestionIdList);

        for (int i = 0; i < oneLevelQuestionList.size(); i++) {
            JSONObject oneLevelQuestionJson = JSONObject.fromObject(oneLevelQuestionList.get(i));
            JSONArray jsonArrayTmp = new JSONArray();
            for (int j = 0; j < twoLevelQuestionList.size(); j++) {
                TwoLevelQuestion twoLevelQuestion = twoLevelQuestionList.get(j);
                if (twoLevelQuestion.getOneLevelQuestionId() == oneLevelQuestionList.get(i).getId()) {
                    jsonArrayTmp.add(JSONObject.fromObject(twoLevelQuestion));
                }
            }
            oneLevelQuestionJson.put("children", jsonArrayTmp);
            resultArr.add(oneLevelQuestionJson);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", exam.get().getId());
        jsonObject.put("title", exam.get().getTitle());
        jsonObject.put("peopleKind", exam.get().getPeopleKind());
        jsonObject.put("tips", exam.get().getTips());
        jsonObject.put("question", resultArr);
        return jsonObject;
    }

    @Transactional
    public void saveRecord(Integer examId, String studentName, String resList, String finishYears) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JSONObject resJson = JSONObject.fromObject(resList);
        JSONArray resJsonList = (JSONArray) resJson.get("data");
        List<Record> recordList = new ArrayList<>();
        String wenquan_id = RandomStringUtils.random(8, "abcdefghijklmnopqrstuvwxyz1234567890");
        for (int i = 0; i < resJsonList.size(); i++) {
            JSONObject object = (JSONObject) resJsonList.get(i);
            Record record = new Record();
            record.setStudentName(studentName);
            record.setTwoLevelQuestionId(object.getInt("id"));
            record.setResult1(object.getString("result1"));
            record.setResult2(object.getString("result2"));
            record.setResult3(object.getString("result3"));
            record.setFinishYears(finishYears);
            record.setCreateTime(df.format(new Date()));
            record.setWenquanId(wenquan_id);
            record.setIsDelete("0");
            recordList.add(record);

//            recordRepository.addRecord(record.getTwoLevelQuestionId(), record.getResult1(), record.getResult2(), record.getResult3(), record.getStudentName());
        }
        batchInsertRepository.batchInsert(recordList);
    }
}


